package com.lzx.demo.bloodpressurehelper.myView;

import android.graphics.Paint;

import java.util.Calendar;
import java.util.Locale;

/**
 *  @author lzx
 *  date:2016/9/1 10:53
 *  描述：自定义日历 view
 */
public class MyCalendarView{
    private Paint mPaint;
    private int widthSize;
    private int heightSize;
    private String[] weeksString;
    private int cellWidth; //每列的宽度
    private Paint mPaintClick;//选中了的天数的按钮

    /*
     * 年月日
     */
    private Calendar cd = Calendar.getInstance(Locale.CHINA);
    private int year;   //年
    private int month;  //月
    private int day;    //日
    private String[][] dateDays;

    private int daysOfMonth;//月天数
    private int weeksOfMonth;//月周数
    private int weekOfFirstDay;//第一天对应的天数


    public MyCalendarView() {
        init();
        initDateDaysWeeksFirstDay();
    }


    private void init(){
        weeksString = new String[]{"日","一","二","三","四","五","六",};
        /*
         *  初始化年月日--默认为当前日期 ps：月份 从 1开始
         */
        this.year = cd.get(Calendar.YEAR);
        this.month = cd.get(Calendar.MONTH)+1;
        this.day = cd.get(Calendar.DATE);
    }

    private void initDateDaysWeeksFirstDay(){
        this.daysOfMonth = CalendarUtils.getDaysOfMonth(year, month);
        this.weeksOfMonth = CalendarUtils.getWeeksOfMonth(year, month);
        this.weekOfFirstDay = CalendarUtils.getWeekOfFirstDay(year, month);
        cellWidth=widthSize/7;
    }

    protected void onDraw() {
        initDateDaysWeeksFirstDay();

        /*
         *  绘制日期
         */
        initDateDaysWeeksFirstDay();
        dateDays=new String[weeksOfMonth][7];//日历数据 -- row为该月的周数 column 为 一周7天
        //填充日历数据
        int day = 1;
        for(int row=0;row<weeksOfMonth;row++){
            for (int column=0;column<7;column++){
                if(row==0){
                    if(column>=(weekOfFirstDay-1)){
                        dateDays[row][column]=day+"";
                        day++;
                    }
                }else if(row==(weeksOfMonth-1)){
                    if(day<=daysOfMonth){
                        dateDays[row][column]=day+"";
                        day++;
                    }
                }else {
                    dateDays[row][column]=day+"";
                    day++;
                }
            }
        }
    }

    /**
     * 切换到下一个月
     */
    public void nextMonth(){
        if(month==11){//如果当前月份为12月，则变成1月并且年份+1
            month=0;
            year++;
        }else {
            month++;
        }
    }

    /**
     * 切换到上一个月
     */
    public void lastMonth(){
        if(month==0){
            month=11;
            year--;
        }else{
            month--;
        }
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public String[][] getDateDays() {
        onDraw();
        return dateDays;
    }

}
